from django.http import HttpResponse
from django.shortcuts import render


from django.http import HttpRequest

def index(request):

    #book.id for book in books
    #book.id for book in bookinfo.objects.all()
    return HttpResponse('deal')

def readbook(request, cat_id, bool_id):
    print(request.GET)



    print(request.GET)

    # request.GET 获取 查询字符串
    # request.GET.get 获取字典数据  字符串字典


    keyword = request.GET.getlist('keyword') #获取多个数据
    print(keyword)
    return HttpResponse('done')

def login(request):
    # form-data 数据 我们是通过 request.POST 来接收
    print(request.POST)

    return HttpResponse('login')



#发送json请求
def login_json(request):
    body = request.body
    print(body)
    #需要对json解码
    body_str = body.decode()

    #将json字符串转换为字典
    import json
    body_dict = json.loads(body_str)
    print(body_dict)
    return HttpResponse('json')


def header(request):
    print(request.method)
    if request.method =='GET':
        print('GET')
    elif request.method == 'post':
        print('POST')
    else:
        print(request.method)

    #获取请求头
    print(request.META)
    """
    {'PATH': '/home/ubuntu/.virtualenvs/py3_django_40/bin:/home/ubuntu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/rabbitmq/sbin', 'LC_MEASUREMENT': 'zh_CN.UTF-8', 'XAUTHORITY': '/run/user/1000/gdm/Xauthority', 'XMODIFIERS': '@im=fcitx', 
    'LC_TELEPHONE': 'zh_CN.UTF-8', 
    'XDG_DATA_DIRS': '/usr/share/ubuntu:/usr/local/share:/usr/share:/var/lib/snapd/desktop',
     'GDMSESSION': 'ubuntu', 
     'MANDATORY_PATH': '/usr/share/gconf/ubuntu.mandatory.path', 
     'LC_TIME': 'zh_CN.UTF-8', 'PAPERSIZE': 'a4', 'TEXTDOMAINDIR': '/usr/share/locale/', 'GTK_IM_MODULE': 'fcitx', 'DBUS_SESSION_BUS_ADDRESS': 'unix:path=/run/user/1000/bus', 'DEFAULTS_PATH': '/usr/share/gconf/ubuntu.default.path', 'PS1': '(py3_django_40) ', 'XDG_CURRENT_DESKTOP': 'ubuntu:GNOME', 'SSH_AGENT_PID': '2161', 'QT4_IM_MODULE': 'fcitx', 'LC_PAPER': 'zh_CN.UTF-8', 'SESSION_MANAGER': 'local/python:@/tmp/.ICE-unix/2070,unix/python:/tmp/.ICE-unix/2070', 'USERNAME': 'ubuntu', 'LOGNAME': 'ubuntu', 'PWD': '/home/ubuntu/Desktop/40/code/bookmanager03', 'IM_CONFIG_PHASE': '2', 'PYCHARM_HOSTED': '1', 'LANGUAGE': 'zh_CN', 'GJS_DEBUG_TOPICS': 'JS ERROR;JS LOG', 'PYTHONPATH': '/home/ubuntu/Desktop/40/code/bookmanager03:/home/ubuntu/software/pycharm-2019.1.3/helpers/pycharm_matplotlib_backend:/home/ubuntu/software/pycharm-2019.1.3/helpers/pycharm_display', 'SHELL': '/bin/bash', 'LC_ADDRESS': 'zh_CN.UTF-8', 'GIO_LAUNCHED_DESKTOP_FILE': '/usr/share/applications/pycharm.desktop', 'OLDPWD': '/home/ubuntu/software/pycharm-2019.1.3/bin', 'GNOME_DESKTOP_SESSION_ID': 'this-is-deprecated', 'GTK_MODULES': 'gail:atk-bridge', 'VIRTUAL_ENV': '/home/ubuntu/.virtualenvs/py3_django_40', 'CLUTTER_IM_MODULE': 'xim', 'TEXTDOMAIN': 'im-config', 'XDG_SESSION_DESKTOP': 'ubuntu', 'SHLVL': '0', 'LC_IDENTIFICATION': 'zh_CN.UTF-8', 'LC_MONETARY': 'zh_CN.UTF-8', 'QT_IM_MODULE': 'fcitx', 'XDG_CONFIG_DIRS': '/etc/xdg/xdg-ubuntu:/etc/xdg', 'LANG': 'zh_CN.UTF-8', 'XDG_SESSION_ID': '2', 'XDG_SESSION_TYPE': 'x11', 'DISPLAY': ':0', 'LC_NAME': 'zh_CN.UTF-8', 'PYCHARM_DISPLAY_PORT': '42981', 'PYTHONIOENCODING': 'UTF-8', 'GPG_AGENT_INFO': '/run/user/1000/gnupg/S.gpg-agent:0:1', 'DESKTOP_SESSION': 'ubuntu', 'USER': 'ubuntu', 'XDG_MENU_PREFIX': 'gnome-', 'GIO_LAUNCHED_DESKTOP_FILE_PID': '88808', 'QT_ACCESSIBILITY': '1', 'WINDOWPATH': '2', 'LC_NUMERIC': 'zh_CN.UTF-8', 'GJS_DEBUG_OUTPUT': 'stderr', 'SSH_AUTH_SOCK': '/run/user/1000/keyring/ssh', 'XDG_SEAT': 'seat0', 'DJANGO_SETTINGS_MODULE': 'bookmanager03.settings', 'PYTHONUNBUFFERED': '1', 'GNOME_SHELL_SESSION_MODE': 'ubuntu', 'XDG_VTNR': '2', 'XDG_RUNTIME_DIR': '/run/user/1000', 'HOME': '/home/ubuntu', 'TZ': 'UTC', 'RUN_MAIN': 'true', 'SERVER_NAME': 'localhost', 'GATEWAY_INTERFACE': 'CGI/1.1', 'SERVER_PORT': '8000', 'REMOTE_HOST': '', 'CONTENT_LENGTH': '', 'SCRIPT_NAME': '', 'SERVER_PROTOCOL': 'HTTP/1.1', 'SERVER_SOFTWARE': 'WSGIServer/0.2', 'REQUEST_METHOD': 'GET', 'PATH_INFO': '/header/', 'QUERY_STRING': '', 'REMOTE_ADDR': '127.0.0.1', 'CONTENT_TYPE': 'text/plain', 'HTTP_USER_AGENT': 'PostmanRuntime/7.13.0', 'HTTP_ACCEPT': '*/*', 'HTTP_CACHE_CONTROL': 'no-cache', 'HTTP_POSTMAN_TOKEN': '42f16f3b-5c5e-4c9f-b8bc-979835ebb826', 'HTTP_HOST': '127.0.0.1:8000', 'HTTP_ACCEPT_ENCODING': 'gzip, deflate', 'HTTP_CONNECTION': 'keep-alive', 'wsgi.input': <django.core.handlers.wsgi.LimitedStream object at 0x7f6414dc5a90>, 'wsgi.errors': <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>, 'wsgi.version': (1, 0), 'wsgi.run_once': False, 'wsgi.url_scheme': 'http', 'wsgi.multithread': True, 'wsgi.multiprocess': False, 'wsgi.file_wrapper': <class 'wsgiref.util.FileWrapper'>}

    """
    http_itcast = request.META['HTTP_ITCAST'] #用[键名]或者字典值
    print(http_itcast)
    return HttpResponse('header')



def detail(request):

    response = HttpResponse(content = '内容', content_type = 'text/html', status = 200)
    #设置响应头数据
    response['itcast'] = 'aaa'

    return response

from django.http import JsonRsponse
def jsonresponse(request):

    userinfo = {'user_id':123,
        'username':'itcast'}
    # data  传递的是字典

    return JsonRsponse(userinfo)


from django.shortcuts import redirect
def to_index(request):

    return redirect('/detail')

    return redirect('http://www.itcast.cn')


from books.models import BookInfo,PeopleInfo

book = BookInfo.objects.get(id=1)
book.peopleinfo_set.all()



# BookInfo.objects.filter(关联模型类名小写__字段__运算符=值)
# __ 2个连续的下划线


BookInfo.objects.filter(peopleinfo__name__exact='郭靖')
#或者
BookInfo.objects.filter(peopleinfo__name='郭靖')

# 查询图书，要求图书中人物的描述包含"八"
BookInfo.objects.filter(peopleinfo__description__contains='八')



#  我们在查询多的数据的时候,条件为1的
# 模型类名.objects.filter(外键__字段__运算符=值)
# 查询书名为“天龙八部”的所有人物

PeopleInfo.objects.filter(book__name='天龙八部')
PeopleInfo.objects.filter(book__name__exact='天龙八部')
# 查询图书阅读量大于30的所有人物
PeopleInfo.objects.filter(book__readcount__gt=30)


def baidu_tieba(request,tieba_id):


    return HttpResponse(tieba_id)


def tieba_register(request,phone):


    return HttpResponse(phone)


def tieba_login(request):

    # 0. 获取用户名
    username=request.GET.get('username')

    response = HttpResponse('set_cookie')

    response.set_cookie(key='username', value=username, max_age=10000)
    response.set_cookie(key='user_id', value='123')
    response.set_cookie(key='phono', value='http://www.baidu.com')

    return response

def get_cookie(request):
    print(request.COOKIES)
    username = request.COOKIES.get('username')

    return HttpResponse(username)

def set_session(request):

    # 1. 设置session
    request.session['user_id']=1234567890

    request.session.set_expiry(None)

    return HttpResponse('set_session')
    return HttpResponse(user_id)


def get_session(request):
    user_id = request.session.get('user_id')


def jd_register(request):

    # GET
    # POST
    print(request.method)
    if request.method == 'GET':
        return HttpResponse('get')
    elif request.method == 'POST':
        return HttpResponse('post')
    else:
        return HttpResponse('other')

    return HttpResponse('jd_register')



from django.views import View
class RegisterView(View):

    # 我们用GET方式 请求的
    def get(self,request):

        return HttpResponse('view get')

    # 我们用POST方式,请求的
    def post(self,request):

        return HttpResponse('view post')



from django.contrib.auth.mixins import LoginRequiredMixin

class CenterView(LoginRequiredMixin,View):
# class CenterView(View):

    def get(self,request):

        return HttpResponse('个人中心 get')


    def post(self,request):


        return HttpResponse('个人中心 post')



